/*
 * Copyright 2010 Groupware.cc All right reserved. This software is the confidential and proprietary information of
 * Groupware.cc ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the license agreement you entered into with Groupware.cc.
 */
package com.rome.syncml.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rome.syncml.constant.ErrorCode;
import com.rome.syncml.domain.User;
import com.rome.syncml.exception.AuthenticationException;
import com.rome.syncml.exception.RegisterException;
import com.rome.syncml.persistence.UserMapper;

/**
 * 类UserService.java的实现描述：TODO 类实现描述
 * 
 * @author rosebible 2011-7-6 上午11:40:44
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public User authenticate(String username, String password) throws AuthenticationException {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new AuthenticationException("MISSING_CREDENTIALS", ErrorCode.MISSING_CREDENTIALS);
        }
        User user = null;
        if (StringUtils.contains(username, "@")) {
            user = userMapper.selectByEmail(username);
        } else {
            user = userMapper.selectByMobile(username);
        }
        if (user != null && StringUtils.equals(password, user.getPassword())) {
            return user;
        } else if (user != null) {
            throw new AuthenticationException("INVALID_CREDENTIALS", ErrorCode.INVALID_CREDENTIALS);
        } else {
            throw new AuthenticationException("NOT_EXISTS_PRINCIPAL", ErrorCode.NOT_EXISTS_PRINCIPAL);
        }
    }

    public long register(String username, String password) throws RegisterException {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new RegisterException("MISSING_CREDENTIALS", ErrorCode.MISSING_CREDENTIALS);
        }
        User user = new User();
        if (StringUtils.contains(username, "@")) {
            User test = userMapper.selectByEmail(username);
            if (test != null) {
                throw new RegisterException("DUPLICATE_PRINCIPAL", ErrorCode.DUPLICATE_PRINCIPAL);
            }
            user.setEmail(username);
        } else {
            User test = userMapper.selectByMobile(username);
            if (test != null) {
                throw new RegisterException("DUPLICATE_PRINCIPAL", ErrorCode.DUPLICATE_PRINCIPAL);
            }
            user.setMobile(username);
        }
        user.setPassword(password);
        return userMapper.insert(user);
    }
    
    public void update(Long userId, String email, String mobile, String password) {
        User user = new User();
        user.setId(userId);
        user.setEmail(email);
        user.setMobile(mobile);
        user.setPassword(password);
        userMapper.updateByPrimaryKeySelective(user);
    }

    public User selectByUsername(String username) {
        User user = null;
        if (StringUtils.contains(username, "@")) {
            user = userMapper.selectByEmail(username);
        } else {
            user = userMapper.selectByMobile(username);
        }
        return user;
    }

    public int countAll() {
        return userMapper.countAll();
    }

    public List<User> selectAll() {
        return userMapper.selectAll();
    }

}
